# include <stdio.h>
# include <stdlib.h>
# include <SDL/SDL.h>
# include "SDL/SDL_mixer.h"
#include <SDL/SDL_image.h>
# include <SDL/SDL_ttf.h>



typedef struct
{
	char key[SDLK_LAST];
	int mousex,mousey;
	int mousexrel,mouseyrel;
	char mousebuttons[8];
        char quit;
} Input;


void UpdateEvents(Input* in)
{
	SDL_Event event;
	while(SDL_PollEvent(&event))
	{
		switch (event.type)
		{
		case SDL_KEYDOWN:
			in->key[event.key.keysym.sym]=1;
			break;
		case SDL_KEYUP:
			in->key[event.key.keysym.sym]=0;
			break;
		case SDL_MOUSEMOTION:
			in->mousex=event.motion.x;
			in->mousey=event.motion.y;
			in->mousexrel=event.motion.xrel;
			in->mouseyrel=event.motion.yrel;
			break;
		case SDL_MOUSEBUTTONDOWN:
			in->mousebuttons[event.button.button]=1;
			break;
		case SDL_MOUSEBUTTONUP:
			in->mousebuttons[event.button.button]=0;
			break;
		case SDL_QUIT:
			in->quit = 1;
			break;
		default:
			break;
		}
	}
}

void background (SDL_Surface *ecran)
{
SDL_Surface *background = NULL ;
SDL_Rect positionbackground;
positionbackground.x = 0;
positionbackground.y = 0;
background= IMG_Load("back/background.jpg");
SDL_BlitSurface(background, NULL, ecran, &positionbackground);
SDL_Flip(ecran);

SDL_FreeSurface(background);
}


void bd (SDL_Surface *ecran)
{
SDL_Surface *bd = NULL ;
SDL_Rect positionbd;
positionbd.x = 600;
positionbd.y = 300;
bd= IMG_Load("back/bd1.png");
SDL_BlitSurface(bd, NULL, ecran, &positionbd);


SDL_FreeSurface(bd);
}



void bg (SDL_Surface *ecran)
{
SDL_Surface *bg = NULL ;
SDL_Rect positionbg;
positionbg.x = 600;
positionbg.y = 300;
bg= IMG_Load("back/bg1.png");
SDL_BlitSurface(bg, NULL, ecran, &positionbg);


SDL_FreeSurface(bg);
}

void bombe (SDL_Surface *ecran)
{
SDL_Surface *bombe = NULL ;
SDL_Rect positionbombe;
positionbombe.x = 300;
positionbombe.y = 300;
bombe= IMG_Load("back/bombe.png");
SDL_BlitSurface(bombe, NULL, ecran, &positionbombe);


SDL_FreeSurface(bombe);
}



void bdd (SDL_Surface *ecran)
{
int i;
SDL_Surface *bdd[2] ;
SDL_Rect positionbdd;
positionbdd.x = 600;
positionbdd.y = 300;
bdd[0]= IMG_Load("back/bd1.png");
bdd[1]=IMG_Load("back/bd2.png");
for(i=0;i<2;i++)
{
SDL_BlitSurface(bdd[i], NULL, ecran, &positionbdd);
SDL_FreeSurface(bdd[2]);
}
}




void bgg (SDL_Surface *ecran)
{
int i;
SDL_Surface *bgg[2] ;
SDL_Rect positionbgg;
positionbgg.x = 300;
positionbgg.y = 270;
bgg[0]= IMG_Load("back/bg1.png");
bgg[1]=IMG_Load("back/bg2.png");
for(i=0;i<2;i++)
{
SDL_BlitSurface(bgg[i], NULL, ecran, &positionbgg);
SDL_FreeSurface(bgg[2]);
}
}



j], NULL, ecran, &positionscore);
positionscore.x=positionscore.x+8;
SDL_Flip(ecran);
SDL_Delay(70);
}
//SDL_FreeSurface(score[22]);
}



void scoreee (SDL_Surface *ecran)
{
int j;
SDL_Surface *scoreee[22] ;
SDL_Rect positionscore;
positionscore.x = 0;
positionscore.y = 300;
scoreee[0]= IMG_Load("back/dude1.png");
scoreee[1]=IMG_Load("back/dude2.png");
scoreee[2]= IMG_Load("back/dude3.png");
scoreee[3]=IMG_Load("back/dude4.png");
scoreee[4]= IMG_Load("back/dude5.png");
scoreee[5]=IMG_Load("back/dude6.png");
scoreee[6]= IMG_Load("back/dude7.png");
scoreee[7]=IMG_Load("back/dude8.png");
scoreee[8]= IMG_Load("back/dude9.png");
scoreee[9]=IMG_Load("back/dude10.png");
scoreee[10]= IMG_Load("back/dude11.png");
scoreee[11]=IMG_Load("back/dude12.png");
scoreee[12]= IMG_Load("back/dude13.png");
scoreee[13]=IMG_Load("back/dude14.png");
scoreee[14]= IMG_Load("back/dude15.png");
scoreee[15]=IMG_Load("back/dude16.png");
scoreee[16]= IMG_Load("back/dude17.png");
scoreee[17]=IMG_Load("back/dude18.png");
scoreee[18]= IMG_Load("back/dude19.png");
scoreee[19]=IMG_Load("back/dude20.png");
scoreee[20]= IMG_Load("back/dude21.png");
scoreee[21]=IMG_Load("back/dude22.png");

for(j=0;j<22;j++)
{
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
background(ecran);
SDL_BlitSurface(scoreee[j], NULL, ecran, &positionscore);
positionscore.x=positionscore.x+18;
SDL_Flip(ecran);
SDL_Delay(70);
}
//SDL_FreeSurface(score[22]);
}



void graff (SDL_Surface *ecran)
{
int i,c=0;
SDL_Surface *graff[2] ;
SDL_Rect positiongraff;
positiongraff.x = 176;
positiongraff.y = 270;
graff[0]= IMG_Load("back/dude4.png");
graff[1]=IMG_Load("back/dude15.png");

while(c<=6)
{
for(i=0;i<2;i++)
{
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
background(ecran);
SDL_BlitSurface(graff[i], NULL, ecran, &positiongraff);
bombe(ecran);
SDL_Flip(ecran);
SDL_Delay(70);
}
c++;
}
}
void graf (SDL_Surface *ecran)
{
int i,c=0;
SDL_Surface *graf[2] ;
SDL_Rect positiongraf;
positiongraf.x = 176;
positiongraf.y = 270;
graf[0]= IMG_Load("back/dude4.png");
graf[1]=IMG_Load("back/dude15.png");

while(c<=1)
{
for(i=0;i<2;i++)
{
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
background(ecran);
SDL_BlitSurface(graf[i], NULL, ecran, &positiongraf);

SDL_Flip(ecran);

}
c++;
}
//SDL_FreeSurface(graf[2]);
}
void texte (SDL_Surface *ecran)
{

 TTF_Font *police = NULL;
SDL_Color couleurNoire = {0, 0, 0},couleurBlanche = {255, 255, 255};
char score[2000] = "";
int scores=400;
SDL_Surface *texte=NULL ;
SDL_Rect positiontexte;
positiontexte.x = 300;
positiontexte.y = 270;
 police = TTF_OpenFont("police/AA.otf", 30);
 sprintf(score, "score : %d", scores);
texte = TTF_RenderText_Shaded(police, score, couleurNoire ,couleurBlanche);
 SDL_BlitSurface(texte, NULL, ecran, &positiontexte);
SDL_Flip(ecran);
SDL_FreeSurface(texte);
}

int main(int argc, char *argv[])
{
SDL_Surface *ecran = NULL;
  TTF_Init();
int test1=0;

ecran = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF| SDL_RESIZABLE);
SDL_WM_SetCaption("BASKETBALL !", NULL);


Input in;
	// init SDL, chargement, tout ce que vous faites avant la boucle.
	memset(&in,0,sizeof(in));
	while(!in.key[SDLK_ESCAPE] && !in.quit)
	{
		UpdateEvents(&in);
		if (in.mousebuttons[SDL_BUTTON_LEFT])
		{
test1=1;
			in.mousebuttons[SDL_BUTTON_LEFT] = 0;
			// fait une seule fois.




		}		

		if (in.key[SDLK_SPACE] )
                {


                     // simplification de la gestion des touches

                }
if(test1==1)
{
background(ecran);
score(ecran);
graff(ecran);
background(ecran);
graf(ecran);
texte(ecran);

test1=0;
}
		
	}
 
 return EXIT_SUCCESS; 
}
